﻿using EFAndMVC.DBLocal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EFAndMVC.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            List<T_UserInfo> list = new List<T_UserInfo>();
            using (var db = new CMSEntities())
            {
                list = (from User in db.T_UserInfo
                        where User.Id > 5
                        select User).ToList();
            }


            //EF添加保存

            //CMSEntities dbfirst = new CMSEntities();
            //T_UserInfo userInfo = new T_UserInfo();
            //userInfo.UserName = "zhangsan";
            //userInfo.UserPwd = "456";
            //userInfo.UserMail = "78@qq.com";
            //dbfirst.T_UserInfo.Add(userInfo);
            //dbfirst.SaveChanges();

            //EF删除
            //CMSEntities dbfirst = new CMSEntities();
            //T_UserInfo userInfo = new T_UserInfo();
            //userInfo.Id = 5;
            //dbfirst.T_UserInfo.Attach(userInfo);
            //dbfirst.Entry(userInfo).State=System.Data.EntityState.Deleted;
            //dbfirst.SaveChanges();

            //EF修改
            //CMSEntities dbfirst = new CMSEntities();
            //T_UserInfo userInfoNew = new T_UserInfo();
            //userInfoNew = (from userInfo in dbfirst.T_UserInfo
            //               where userInfo.Id == 4
            //               select userInfo).FirstOrDefault();
            //userInfoNew.UserName = "lisi";
            //dbfirst.Entry(userInfoNew).State = System.Data.EntityState.Modified;
            //dbfirst.SaveChanges();
            return View(list);
        }

        public ActionResult GetName()
        {
            GetUserName(Name: "Lisi");
            return View();
        }

        public string GetUserName(string Name = null)
        {
            Name = "zhangsan";
            return Name;
        }
    }
}
